package com.game.tank;

import com.game.tank.bean.Bullet;
import com.game.tank.bean.Explode;
import com.game.tank.bean.Tank;
import com.game.tank.enums.Dir;
import com.game.tank.enums.TankGroup;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TankFrame extends Frame {

    Tank myTank = new Tank(200, 400, Dir.UP, TankGroup.GOOD,this);
    public List<Bullet> bullets = new ArrayList<>();

    public List<Tank> tanks = new ArrayList<>();

    public List<Explode> explodes = new ArrayList<>();

    public Explode explode = new Explode(50, 50, this);

    public static final int GAME_WIDTH = 800,GAME_HEIGHT = 600;
    public TankFrame() {
        setSize(GAME_WIDTH,GAME_HEIGHT);
        setResizable(false);
        setVisible(true);
        setTitle("TANK WAR");

        addKeyListener(new MyKeyListener());
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public void paint(Graphics graphics) {
        myTank.paint(graphics);

        Color c = graphics.getColor();

        graphics.setColor(Color.WHITE);
        graphics.drawString("敌军的数量：" + tanks.size(), 50, 50);
        graphics.setColor(c);

        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).paint(graphics);
        }

        for (int i = 0; i < tanks.size(); i++) {
            tanks.get(i).paint(graphics);
        }

        for (int i = 0; i < explodes.size(); i++) {
            explodes.get(i).paint(graphics);
        }

        for (int i = 0; i < bullets.size(); i++) {
            for (int j = 0; j < tanks.size(); j++) {
                bullets.get(i).collectWidth(tanks.get(j));
            }
        }


    }

    class MyKeyListener extends KeyAdapter {
        boolean bl,br,bu,bd;
        @Override
        public void keyPressed(KeyEvent e) {

            switch (e.getKeyCode()) {
                case KeyEvent.VK_UP:
                    bu = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bd = true;
                    break;
                case KeyEvent.VK_LEFT:
                    bl = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    br = true;
                    break;
                default:
                    break;
            }
            setMainTankDir();


        }

        private void setMainTankDir() {

            if (!bl && !br && !bu && !bd) {
                myTank.setMoving(false);
            }else {
                myTank.setMoving(true);
                if (bl) myTank.setDir(Dir.LEFT);
                if (br) myTank.setDir(Dir.RIGHT);
                if (bu) myTank.setDir(Dir.UP);
                if (bd) myTank.setDir(Dir.DOWN);
            }


        }

        @Override
        public void keyReleased(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_UP:
                    bu = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bd = false;
                    break;
                case KeyEvent.VK_LEFT:
                    bl = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    br = false;
                    break;
                case KeyEvent.VK_X:
                    myTank.fire();
                    break;
                default:
                    break;
            }
            setMainTankDir();
        }
    }

    Image offScreenImage = null;

    @Override
    public void update(Graphics graphics) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics offScreen = offScreenImage.getGraphics();
        Color c = offScreen.getColor();
        offScreen.setColor(Color.BLACK);
        offScreen.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);
        offScreen.setColor(c);
        paint(offScreen);
        graphics.drawImage(offScreenImage, 0, 0, null);
    }
}
